package zapx

import (
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	Logger      *zap.Logger
	sugarLogger *zap.SugaredLogger
)

func InitLogger() {
	Logger, _ = zap.NewDevelopment()
	//Logger, _ = zap.NewProduction()
	sugarLogger = Logger.Sugar()
}

func InitLoggerV1() {
	encoderConfig:=zapcore.EncoderConfig{
		TimeKey:        "ts",
		LevelKey:       "level",
		NameKey:        "Logger",
		CallerKey:      "caller",
		FunctionKey:    zapcore.OmitKey,
		MessageKey:     "msg",
		StacktraceKey:  "stacktrace",
		LineEnding:     zapcore.DefaultLineEnding,
		EncodeLevel:    zapcore.LowercaseLevelEncoder,
		EncodeTime:     zapcore.ISO8601TimeEncoder,
		EncodeDuration: zapcore.SecondsDurationEncoder,
		EncodeCaller:   zapcore.ShortCallerEncoder,
	}
	//encoder := zapcore.NewJSONEncoder(encoderConfig)
	encoder := zapcore.NewConsoleEncoder(encoderConfig)
	//file, _ := os.Create("./test.log")
	//file, _ := os.OpenFile("./test.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0744)
	//writeSyncer := zapcore.AddSync(file)
	writeSyncer:=getLoggerWriter()
	core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)

	Logger = zap.New(core, zap.AddCaller())
}

func getLoggerWriter() zapcore.WriteSyncer {
	lumberJackLogger := &lumberjack.Logger{
		Filename: "./test.log",
		MaxSize: 1,	// M
		MaxBackups: 5, // 备份数量
		MaxAge: 3, // 保存天数
		Compress: false,	// 是否压缩
	}
	return zapcore.AddSync(lumberJackLogger)
}
